﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sms_modem_control
{
    public partial class frmSetting : Form
    {
        ApplicationSettings appSettings;
        public frmSetting()
        {
            InitializeComponent();
            appSettings = new ApplicationSettings();
        }
        /// <summary>
        /// Fill list values
        /// </summary>
        private void fillListValues(){
            //
            foreach (string strVal in appSettings.ComSpeedList)
                cboComSpeedList.Items.Add(strVal);
            //
            foreach (string strVal in appSettings.IPProtocolList)
                cboIPProtocolList.Items.Add(strVal);
            //
            foreach (string strVal in appSettings.ModemProtocolList)
                cboModemProtocolList.Items.Add(strVal);
            //
            foreach (string strVal in appSettings.ModemList)
                cboModemList.Items.Add(strVal);       

            //Search COM port exist in computer machine
            /*List<sms_modem_control.Win32DeviceMgmt.DeviceInfo> deviceInfo = Win32DeviceMgmt.GetAllModem();
            foreach (sms_modem_control.Win32DeviceMgmt.DeviceInfo strVal in deviceInfo)
            {
                cboComportList.Items.Add(strVal.name);
                
            }*/
            cboComportList.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());

        }

        /// <summary>
        /// Fill user setting values
        /// </summary>
        private void fillValue() {
            txtComPort.Text = appSettings.ComIPPortUserChose;
            txtMarkComIP.Text = appSettings.ComIPUserChose;
            txtModemName.Text = appSettings.ModemNameUserChose;

            cboModemList.Text = appSettings.ModemUserChose;
            cboIPProtocolList.Text = appSettings.IPProtocolUserChose;
            cboComSpeedList.Text = appSettings.ComSpeedUserChose;
            cboModemProtocolList.Text = appSettings.ModemProtocolUserChose;
            cboComportList.Text = appSettings.ComUserChose;

        }
        /// <summary>
        /// Load settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSetting_Load(object sender, EventArgs e)
        {
            fillListValues();
            fillValue();
        }
        /// <summary>
        /// Save user settings
        /// </summary>
        private void saveSettings() {
            appSettings.ComIPPortUserChose = txtComPort.Text;
            appSettings.ComIPUserChose =txtMarkComIP.Text ;
            appSettings.ModemNameUserChose = txtModemName.Text ;

             appSettings.ModemUserChose=cboModemList.Text;
             appSettings.IPProtocolUserChose=cboIPProtocolList.Text;
             appSettings.ComSpeedUserChose=cboComSpeedList.Text;
             appSettings.ModemProtocolUserChose=cboModemProtocolList.Text;
             appSettings.ComUserChose=cboComportList.Text ;

             appSettings.Save();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            saveSettings();
            this.Close();
        }
    }
}
